package com.dodo.videoEdit.ui.fragment.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import com.dodo.videoEdit.R;
import com.dodo.videoEdit.databinding.FragmentFileChoseItemBinding;
import com.dodo.videoEdit.greendao.FileDirType;
import com.ramotion.foldingcell.FoldingCell;

import java.util.HashSet;
import java.util.List;

/**
 * Simple example of ListAdapter for using with Folding Cell
 * Adapter holds indexes of unfolded elements for correct work with default reusable views behavior
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public class FoldingCellListAdapter extends ArrayAdapter<FileDirType> {

    private HashSet<Integer> unfoldedIndexes = new HashSet<>();
    private View.OnClickListener defaultRequestBtnClickListener;
    private int type = 0;
    public FoldingCellListAdapter(Context context, List<FileDirType> objects,int type) {
        super(context, 0, objects);
        this.type = type;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        // get item for selected view
        FileDirType item = getItem(position);
        FragmentFileChoseItemBinding binding = DataBindingUtil.getBinding(convertView);
        // if cell is exists - reuse it, if not - create the new one from resource
        FoldingCell cell = (FoldingCell) convertView;
        ViewHolder viewHolder;
        if (cell == null) {
            viewHolder = new ViewHolder();
            LayoutInflater vi = LayoutInflater.from(getContext());
            cell = (FoldingCell) vi.inflate(R.layout.fragment_file_chose_item, parent, false);
            // binding view parts to view holder
            viewHolder.name = cell.findViewById(R.id.dir_name);
            viewHolder.name2 = cell.findViewById(R.id.dir_name2);
            viewHolder.num = cell.findViewById(R.id.file_num);
            viewHolder.size = cell.findViewById(R.id.file_size);
            viewHolder.button = cell.findViewById(R.id.open_button);
            cell.setTag(viewHolder);
        } else {
            // for existing cell set valid valid state(without animation)
            if (unfoldedIndexes.contains(position)) {
                cell.unfold(true);
            } else {
                cell.fold(true);
            }
            viewHolder = (ViewHolder) cell.getTag();
        }

        if (null == item)
            return cell;

        // bind data from selected element to view through view holder
        viewHolder.name.setText(item.getPathDir());
        viewHolder.name2.setText(item.getPathDir());
        int num = 0;
        switch (type) {
            case 1 :
                num = item.getAudioCount();
                break;
            case 2 :
                num = item.getImageCount();
                break;
            case 3 :
                num = item.getVideoCount();
                break;
        }
        viewHolder.num.setText("文件数量: " +num);

        // set custom btn handler for list item from that item
        /*if (item.getRequestBtnClickListener() != null) {
            viewHolder.button.setOnClickListener(item.getRequestBtnClickListener());
        } else {
            // (optionally) add "default" handler if no handler found in item
            viewHolder.button.setOnClickListener(defaultRequestBtnClickListener);
        }*/
        FoldingCell finalCell = cell;
        cell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalCell.toggle(false);
            }
        });
        return cell;
    }

    // simple methods for register cell state changes
    public void registerToggle(int position) {
        if (unfoldedIndexes.contains(position))
            registerFold(position);
        else
            registerUnfold(position);
    }

    public void registerFold(int position) {
        unfoldedIndexes.remove(position);
    }

    public void registerUnfold(int position) {
        unfoldedIndexes.add(position);
    }

    public View.OnClickListener getDefaultRequestBtnClickListener() {
        return defaultRequestBtnClickListener;
    }

    public void setDefaultRequestBtnClickListener(View.OnClickListener defaultRequestBtnClickListener) {
        this.defaultRequestBtnClickListener = defaultRequestBtnClickListener;
    }

    // View lookup cache
    private static class ViewHolder {
        TextView name;
        TextView name2;
        TextView size;
        TextView num;
        TextView button;
    }
}
